package com.wolfword.crawler.server.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wolfword.common.enums.ClientStatusEnum;
import com.wolfword.common.util.object.StringUtil;
import com.wolfword.crawler.server.dao.ClientLogDao;
import com.wolfword.crawler.server.dao.RequestLogDao;
import com.wolfword.crawler.server.dto.RequestLogDto;
import com.wolfword.crawler.server.entity.ClientLogEntity;
import com.wolfword.crawler.server.entity.RequestLogEntity;
import com.wolfword.crawler.server.service.RequestLogService;

@Service
public class RequestLogServiceImpl implements RequestLogService {
	
	@Autowired
	private RequestLogDao requestLogDao;
	@Autowired
	private ClientLogDao clientLogDao;

	public RequestLogDao getRequestLogDao() {
		return requestLogDao;
	}

	@Resource
	public void setRequestLogDao(RequestLogDao requestLogDao) {
		this.requestLogDao = requestLogDao;
	}

	/**
	 * 插入数据
	 *
	 * @param entity
	 *            实体类
	 * @return 主键值
	 */
	@Override
	@Transactional
	public long insert(RequestLogEntity entity) {
		String clientIp = entity.getClientIp();
		Integer clientPort = entity.getClientPort();
		if (StringUtil.isEmpty(clientIp,clientPort)) {
			return -1;
		}
		
		//1.查看此客户端是否有过连接，有则修改状态，无则新增
		ClientLogEntity clientLogEntity = clientLogDao.get4IpAndPort(clientIp, clientPort);
		if (clientLogEntity != null) {
			clientLogEntity.setClientStatus(ClientStatusEnum.WORKING.getStatus());//工作中
			clientLogEntity.setUpdateTime(new Date());
			clientLogDao.updateByPrimary(clientLogEntity);
		}else {
			clientLogEntity = new ClientLogEntity();
			clientLogEntity.setClientIp(clientIp);
			clientLogEntity.setClientPort(clientPort);
			clientLogEntity.setClientName(entity.getClientName());
			clientLogEntity.setClientStatus(ClientStatusEnum.WORKING.getStatus());//工作中
			clientLogEntity.setUpdateTime(new Date());
			clientLogDao.insert(clientLogEntity);
		}
		
		//2.插入新记录
		requestLogDao.insert(entity);
		
		return entity.getId();
	}

	/**
	 * 批量插入数据
	 *
	 * @param list
	 *            实体类
	 */
	@Override
	public void batchInsert(List<RequestLogEntity> list) {
		requestLogDao.batchInsert(list);
	}

	/**
	 * 根据主键更新数据
	 *
	 * @param entity
	 *            新数据
	 */
	@Override
	public Integer updateByPrimary(RequestLogEntity entity) {
		return requestLogDao.updateByPrimary(entity);
	}

	/**
	 * 根据主键获取数据
	 *
	 * @param entity
	 *            查询条件
	 * @return 查询结果
	 */
	@Override
	public RequestLogEntity getByPrimary(RequestLogEntity entity) {
		return requestLogDao.getByPrimary(entity);
	}

	/**
	 * 根据查询条件获取一条记录
	 *
	 * @param conditions
	 *            查询条件
	 * @return 查询结果
	 */
	@Override
	public RequestLogEntity getOne(Map<String, Object> conditions) {
		return requestLogDao.getOne(conditions);
	}

	/**
	 * 根据查询条件查询
	 *
	 * @param conditions
	 *            查询条件
	 * @return 结果集
	 */
	@Override
	public List<RequestLogEntity> queryList(Map<String, Object> conditions) {
		return requestLogDao.queryList(conditions);
	}

	/**
	 * 查询所有数据
	 *
	 * @return 结果集
	 */
	@Override
	public List<RequestLogEntity> getAll() {
		return requestLogDao.getAll();
	}
}
